package controller;

import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.BookDetailsDAO;
import DAO.DAOFactory;
import DAO.FacilitiesDAO;
import DAO.NotFoundException;

import model.BookDetailsDTO;
import model.FacilitiesDTO;

/**
 * Servlet implementation class MakeABooking
 */
@WebServlet("/MakeABooking")
public class MakeABookingFilter extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MakeABookingFilter() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doProcess(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doProcess(request,response);
	}
	protected void doProcess(HttpServletRequest request,HttpServletResponse response)
	        throws ServletException,IOException
	{
		ArrayList<FacilitiesDTO> facilitiesList=new ArrayList<FacilitiesDTO>();
		ArrayList<BookDetailsDTO> bookDetailsList= new ArrayList<BookDetailsDTO>();
		FacilitiesDAO fa=DAOFactory.loadInstance().getFacilitiesDAO();
		BookDetailsDAO bo=DAOFactory.loadInstance().getBookDetailsDAO();
		
		String type="";
		String fromDate="";
		String toDate="";
		
		if ((request.getParameter("type")!=null)&&
				(request.getParameter("fromDate")!=null)&&
				(request.getParameter("toDate")!=null)){
			type = request.getParameter("type");
			fromDate= request.getParameter("fromDate");
			toDate= request.getParameter("toDate");
		}
		
		System.out.println("type: "+type);
		System.out.println("before fromDate: "+fromDate);
		System.out.println("before toDate: "+toDate);
		
		//Change fromDate and toDate to Date type
		Date fDate = null;
		Date tDate = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		   try {
		    fDate = df.parse(fromDate);
		    System.out.println("After fromDate: "+df.format(fDate));
		    tDate = df.parse(toDate);
		    System.out.println("After toDate: "+df.format(tDate));
		   } catch (ParseException e) {
		    e.printStackTrace();
		   }
		//Compare the facilities and bookingDetails in 2-level for loop and filter the available facilities.
		try {
			if ( type != null ) {
				facilitiesList=fa.FindByType(type);
				bookDetailsList=bo.findAll();
				
				for (int i=0;i<facilitiesList.size();i++){
					for (int j=0;j<bookDetailsList.size();j++){
						
						//1. To see whether the name is same,if same compare the date:
						if (facilitiesList.get(i).getFacilityName().equals(bookDetailsList.get(j).getFacilityName())){
							System.out.println("RoomName same!");
							Date fDateInRecord=null;
							Date tDateInRecord=null;
							try {
								fDateInRecord=df.parse(bookDetailsList.get(j).getFromDate().toString());
								tDateInRecord=df.parse(bookDetailsList.get(j).getToDate().toString());
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							//2. if date overlaps:
							if ( ((fDate.compareTo(fDateInRecord)<=0)&&(tDate.compareTo(tDateInRecord)>=0))||
								 ((fDate.compareTo(fDateInRecord)>=0)&&(tDate.compareTo(tDateInRecord)<=0))||
								 ((fDate.compareTo(fDateInRecord)<=0)&&(tDate.compareTo(fDateInRecord)>=0))||
								 ((fDate.compareTo(tDateInRecord)<=0)&&(tDate.compareTo(tDateInRecord)>=0)))
								{
								//3. if status is valid (Book, Update, Approve), remove the facilities:
								if (bookDetailsList.get(j).getStatus().equals("Approve")){
								facilitiesList.remove(i);
								System.out.println("remove same room with overlappedtime!");
								}
							} 
						}
					}
				}
				
				request.setAttribute("list",facilitiesList);
				request.setAttribute("bookDetails", bookDetailsList);
				request.setAttribute("fromDate",fromDate);
				request.setAttribute("toDate",toDate);
				request.setAttribute("type",type);
			}
			
		} catch (NotFoundException e)
		{
			e.printStackTrace();
		}
		
		RequestDispatcher rd=request.getRequestDispatcher("/MakeABooking.jsp");
		try {
			rd.forward(request, response);
			} 
		catch (ServletException e) {
			e.printStackTrace();
			} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	

}
